package com.etc.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.dao.impl.BussiteDaoImpl;
import com.etc.entity.BusSites;
import com.etc.entity.Bussite;
import com.etc.entity.Site;
import com.etc.service.impl.BussiteServiceImpl;
import com.etc.service.impl.SiteServiceImpl;
import com.etc.util.PageData;
import com.google.gson.Gson;

/**
 * Servlet implementation class SiteController
 */
@WebServlet("/SiteController")
public class SiteController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SiteController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("add".equals(op)) {
			addSite(request,response);
		}else if ("show".equals(op)) {
			showSite(request,response);
		}else if ("addbussite".equals(op)) {
			addbusite(request,response);
		}else if ("ajaxshow".equals(op)) {
			ajaxshow(request,response);
		}else if ("rmv".equals(op)) {
			ajaxrmv(request,response);
		}else if ("up".equals(op)) {
			ajaxup(request,response);
		}else if ("ajaxsite".equals(op)) {
			ajaxsite(request,response);
		}else if ("ajaxdel".equals(op)) {
			ajaxdel(request,response);
		}
	}

	





	







	SiteServiceImpl impl = new SiteServiceImpl();
	BussiteServiceImpl bsi = new BussiteServiceImpl();
	private void addSite(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 增加站点
		String name = request.getParameter("b_name");
		if (name.trim().length()!=0) {
		Site site = new Site(name);
		System.out.println(site);
		boolean b = impl.addSite(site);
		System.out.println(b);
		//发送到页面
		Gson gson = new Gson();
		String json = gson.toJson(b);
		response.getWriter().print(json);
		response.getWriter().close();
		}else {
			response.sendRedirect("back/addbusSites.jsp");
		}
	}

	

	private void showSite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//查询站点信息
		
		List<BusSites> list = bsi.queryAllbusSite();
		//讲查询的数据保存
		request.setAttribute("site", list);
		//跳转页面
		request.getRequestDispatcher("back/bussite-information.jsp").forward(request, response);
	}
	
	private void addbusite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//增加中间关联站点
		String bid = request.getParameter("b_id");
		String sid = request.getParameter("s_id");
		if (bid.trim().length()!=0 && sid.trim().length()!=0) {
		
		String location = request.getParameter("s_location");
		Bussite bussite = new Bussite(Integer.parseInt(bid), Integer.parseInt(sid), Integer.parseInt(location));
		boolean b = bsi.addBussite(bussite);
		//跳转页面
		if (b) {
			request.getRequestDispatcher("SiteController?op=show").forward(request, response);
		}else {
			response.sendRedirect("back/addbussite.jsp");
		}
		}else {
			response.sendRedirect("back/addbussite.jsp");
		}
	}
	
	private void ajaxshow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//编码格式
		response.setContentType("application/json;charset=utf-8");
		
		//默认页码和显示条数
		int pageNo=1;
		int pageSize=3;
		if (request.getParameter("pageNo")!=null) {
			pageNo=Integer.parseInt(request.getParameter("pageNo"));
		}
		if (request.getParameter("pageSize")!=null) {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		//调用业务层
		  PageData list = bsi.queryLimtSite(pageNo, pageSize);
		//传值到前端页面
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response.getWriter().print(json);
		response.getWriter().close();
	}
	private void ajaxrmv(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//取值
		String sid = request.getParameter("sid");
		String location = request.getParameter("location");
		BussiteDaoImpl dao = new BussiteDaoImpl();
		int id = dao.queryBussiteId(Integer.parseInt(sid), Integer.parseInt(location));
		//调用业务层
		boolean b = bsi.deleBussite(id);
		Gson gson = new Gson();
		String json = gson.toJson(b);
		response.getWriter().print(json);
		response.getWriter().close();
		
	}
	
	private void ajaxup(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//编码格式
		response.setContentType("application/json;charset=utf-8");
		//取值
		String s_id = request.getParameter("sid");
		System.out.println(s_id);
		String b_id = request.getParameter("bid");
		System.out.println(b_id);
		String s_location = request.getParameter("location");
		System.out.println(s_location);
		//调用业务层
		int bs_id = bsi.queryBussiteId(Integer.parseInt(b_id),  Integer.parseInt(s_location));
		System.out.println(bs_id);
		Bussite bussite = new Bussite(bs_id, Integer.parseInt(b_id), Integer.parseInt(s_id), Integer.parseInt(s_location));
		System.out.println(bussite);
		
		//修改
		boolean b = bsi.updatBussite(bussite);
		//new Gson对象
		Gson gson = new Gson();
		String json = gson.toJson(b);
		response.getWriter().print(json);
		response.getWriter().close();
	}
	
	private void ajaxsite(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//编码格式
				response.setContentType("application/json;charset=utf-8");
				
				//默认页码和显示条数
				int pageNo=1;
				int pageSize=3;
				if (request.getParameter("pageNo")!=null) {
					pageNo=Integer.parseInt(request.getParameter("pageNo"));
				}
				if (request.getParameter("pageSize")!=null) {
					pageSize=Integer.parseInt(request.getParameter("pageSize"));
				}
				//调用业务层
		// 查询站点信息
		PageData list = impl.querySite(pageNo, pageSize);
		//传值到前端页面
				Gson gson = new Gson();
				String json = gson.toJson(list);
				response.getWriter().print(json);
				response.getWriter().close();
	}
	private void ajaxdel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 取值
		String sid = request.getParameter("sid");
		//调用业务层删除
		boolean b = impl.rmvSite(Integer.parseInt(sid));
		Gson gson = new Gson();
		String json = gson.toJson(b);
		response.getWriter().print(json);
		response.getWriter().close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	

}
